package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dao.SysPostMenuDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPostMenu;
import com.ruoyi.system.service.ISysPostMenuService;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;

/**
 * 岗位与菜单关系信息Service业务层处理
 * 
 * @author youde
 * @date 2022-11-24
 */
@Service
public class SysPostMenuServiceImpl implements ISysPostMenuService 
{
    @Autowired
    private SysPostMenuDAO sysPostMenuDAO;
    

    /**
     * 查询岗位与菜单关系信息
     * 
     * @param postMenuId 岗位与菜单关系信息主键
     * @return 岗位与菜单关系信息
     */
    @Override
    public SysPostMenu selectSysPostMenuByPostMenuId(String postMenuId)
    {
        return sysPostMenuDAO.findById(postMenuId).orElse(null);
    }

    /**
     * 查询岗位与菜单关系信息列表
     * 
     * @param sysPostMenu 岗位与菜单关系信息
     * @return 岗位与菜单关系信息
     */
    @Override
    public Page<SysPostMenu> selectSysPostMenuList(SysPostMenu sysPostMenu,Pageable pageinfo)
    {
        return sysPostMenuDAO.findAll(getSpecification(sysPostMenu),pageinfo);
    }
    
        /**
     * 查询岗位与菜单关系信息列表
     * 
     * @param sysPostMenu 岗位与菜单关系信息
     * @return 岗位与菜单关系信息
     */
    @Override
    public List<SysPostMenu> selectSysPostMenuList(SysPostMenu sysPostMenu)
    {
        return sysPostMenuDAO.findAll(getSpecification(sysPostMenu));
    }
    
    
    private Specification<SysPostMenu> getSpecification(SysPostMenu sysPostMenu){
        return (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
			
            if (!StringUtils.isEmpty(sysPostMenu.getPostId())) {
              predicateList.add(criteriaBuilder.equal(root.get("postId"), sysPostMenu.getPostId()));
            }
            if (!StringUtils.isEmpty(sysPostMenu.getMenuId())) {
              predicateList.add(criteriaBuilder.equal(root.get("menuId"), sysPostMenu.getMenuId()));
            }
            if (!StringUtils.isEmpty(sysPostMenu.getMenuType())) {
              predicateList.add(criteriaBuilder.equal(root.get("menuType"), sysPostMenu.getMenuType()));
            }
          Predicate[] p = new Predicate[predicateList.size()];
          return criteriaBuilder.and(predicateList.toArray(p));
        };
    
    }

    /**
     * 新增岗位与菜单关系信息
     * 
     * @param sysPostMenu 岗位与菜单关系信息
     * @return 结果
     */
    @Transactional
    @Override
    public List<SysPostMenu> insertSysPostMenu(SysPostMenu[] sysPostMenu)
    {
        sysPostMenuDAO.deleteByPostId(sysPostMenu[0].getPostId());
        return sysPostMenuDAO.saveAll(Arrays.asList(sysPostMenu));
    }

    /**
     * 修改岗位与菜单关系信息
     * 
     * @param sysPostMenu 岗位与菜单关系信息
     * @return 结果
     */
    @Override
    public SysPostMenu updateSysPostMenu(SysPostMenu sysPostMenu)
    {
        return sysPostMenuDAO.save(sysPostMenu);
    }

    /**
     * 批量删除岗位与菜单关系信息
     * 
     * @param postMenuIds 需要删除的岗位与菜单关系信息主键
     * @return 结果
     */
    @Override
    public void deleteSysPostMenuByPostMenuIds(String[] postMenuIds)
    {
        sysPostMenuDAO.deleteAllById(Arrays.asList(postMenuIds));
    }

    /**
     * 删除岗位与菜单关系信息信息
     * 
     * @param postMenuId 岗位与菜单关系信息主键
     * @return 结果
     */
    @Override
    public void deleteSysPostMenuByPostMenuId(String postMenuId)
    {
        sysPostMenuDAO.deleteById(postMenuId);
    }

    /**
     * 根据岗位id删除分配的菜单信息
     *
     * @param postId
     * @return
     */
    @Transactional
    @Override
    public int deleteByPostId(String postId) {
        return sysPostMenuDAO.deleteByPostId(postId);
    }
}
